//
//  NewFeatureController.swift
//  WBDemo
//
//  Created by Jin on 27/04/2018.
//  Copyright © 2018 com.baidu.123. All rights reserved.
//

import UIKit
import SnapKit

class NewFeatureController: UIViewController {

    @IBOutlet weak var collectionView:UICollectionView!
    //新特性界面总数
    let maxImageCount = 4
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        collectionView.dataSource = self
        collectionView.delegate = self
    }

}

extension NewFeatureController:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return maxImageCount
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "newfeatureCell", for: indexPath) as! JCollectionViewCell
        //cell.backgroundColor = (indexPath.item % 2) == 0 ? UIColor.red : UIColor.green
        cell.index = indexPath.item
        
        return cell
    }
}

extension NewFeatureController:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        //该方法传递给我们的是上一页的索引
        
    }
}

//自定义cell
class JCollectionViewCell:UICollectionViewCell{
    
    var index = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            
            startBtn.isHidden = true
            if index == 3 {
                startBtn.isHidden = false
                
                //处理动画时先禁用按钮交互
                startBtn.isUserInteractionEnabled = false
                startBtn.transform = CGAffineTransform(scaleX: 0.0, y: 0.0)
                //usingSpringWithDamping:范围0.0f-1.0f,越小,震动效果越明显
                //initialSpringVelocity:表示初始速度,数值越大,一开始移动速度越快
                UIView.animate(withDuration: 2.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10.0, options: UIViewAnimationOptions(rawValue: 0), animations: {
                    self.startBtn.transform = CGAffineTransform.identity
                }) { (isComplete) in
                    self.startBtn.isUserInteractionEnabled = true
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
        //fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 内部控制方法
    private func setupUI(){
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        /*
        iconView.translatesAutoresizingMaskIntoConstraints = false
        var cons = NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[iconView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["iconView":iconView])
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[iconView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["iconView":iconView])
        contentView.addConstraints(cons)
        */
        
        iconView.snp.makeConstraints { (make) in
            make.top.bottom.left.right.equalToSuperview()
        }
        startBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(contentView.snp.bottom).offset(-160)
        }
        
    }
    
    @objc private func startBtnClick(){
        JLog(message: "")
    }
    
    // MARK: - 懒加载
    private lazy var iconView = UIImageView()
    private lazy var startBtn:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), for: .highlighted)
        btn.addTarget(self, action: #selector(startBtnClick), for: .touchUpInside)
        btn.sizeToFit()
        return btn
    }()
}

//自定义布局
class JFlowLayout:UICollectionViewFlowLayout{
    //准备布局
    override func prepare() {
        super.prepare()
        itemSize = (collectionView?.bounds.size)!
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        collectionView?.bounces = false
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
        scrollDirection = UICollectionViewScrollDirection.horizontal
    }
}
